<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'

// 创建表单数据模型
const form = ref({
  gamesName: '',
  applyMaterials: null as File | null
})

// 创建响应式状态
const loading = ref(false)
const errorMessage = ref('')
const fileInput = ref<HTMLInputElement | null>(null)

// 获取路由实例
const router = useRouter()

// 定义 emits
const emit = defineEmits(['cancel'])

// 处理文件选择
const handleFileChange = (event: Event) => {
  const target = event.target as HTMLInputElement
  if (target.files && target.files.length > 0) {
    form.value.applyMaterials = target.files[0]
  } else {
    form.value.applyMaterials = null
  }
}

// 提交表单
const submitForm = async () => {
  // 简单验证
  if (!form.value.gamesName.trim()) {
    errorMessage.value = '赛事名称不能为空'
    return
  }
  
  if (!form.value.applyMaterials) {
    errorMessage.value = '请上传申请材料'
    return
  }
  
  try {
    loading.value = true
    errorMessage.value = ''
    
    // 从 localStorage 获取 token
    const token = localStorage.getItem('token')
    
    if (!token) {
      errorMessage.value = '未找到身份凭证，请重新登录'
      return
    }
    
    // 创建 FormData 对象
    const formData = new FormData()
    formData.append('gamesName', form.value.gamesName)
    formData.append('file', form.value.applyMaterials)
    
    // 发送 POST 请求
    const response = await fetch('http://localhost:8080/quaApplication/submit', {
      method: 'POST',
      headers: {
        'token': token
      },
      body: formData
    })
    
    const result = await response.json()
    
    if (result.success) {
      // 提交成功，返回申办资格审核列表页面
      alert('申办资格提交成功')
      router.back()
    } else {
      errorMessage.value = result.message || '提交失败'
    }
  } catch (err) {
    console.error('提交申办资格失败:', err)
    errorMessage.value = '提交失败，请稍后重试'
  } finally {
    loading.value = false
  }
}

// 返回申办资格审核列表页面
const goBack = () => {
  // 发射取消事件，通知父组件切换视图
  emit('cancel')
}

// 清除已选择的文件
const clearFile = () => {
  if (fileInput.value) {
    fileInput.value.value = ''
    form.value.applyMaterials = null
  }
}
</script>

<template>
  <div class="new-qua-app-container">
    <h2>新建申办资格</h2>
    
    <div class="form-container">
      <div class="form-group">
        <label for="gamesName">赛事名称:</label>
        <input 
          id="gamesName"
          v-model="form.gamesName"
          type="text" 
          placeholder="请输入赛事名称"
          class="form-input"
        />
      </div>
      
      <div class="form-group">
        <label for="applyMaterials">申请材料:</label>
        <input
          id="applyMaterials"
          ref="fileInput"
          type="file"
          @change="handleFileChange"
          class="form-file"
        />
        <div v-if="form.applyMaterials" class="file-info">
          已选择文件: {{ form.applyMaterials.name }}
          <button @click="clearFile" class="clear-file-button">清除</button>
        </div>
      </div>
      
      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
      
      <div class="form-actions">
        <button 
          @click="goBack"
          class="cancel-button"
          :disabled="loading"
        >
          取消
        </button>
        <button 
          @click="submitForm"
          class="submit-button"
          :disabled="loading"
        >
          {{ loading ? '提交中...' : '提交' }}
        </button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.new-qua-app-container {
  width: 95%;
  padding: 20px;
}

.new-qua-app-container h2 {
  color: #333;
  font-size: 2rem;
  margin-bottom: 20px;
  text-align: center;
}

.form-container {
  max-width: 600px;
  margin: 0 auto;
  background: white;
  padding: 30px;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
  color: #555;
}

.form-input,
.form-file {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 16px;
  box-sizing: border-box;
}

.form-input:focus,
.form-file:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 5px rgba(52, 152, 219, 0.3);
}

.file-info {
  margin-top: 10px;
  padding: 10px;
  background-color: #f8f9fa;
  border-radius: 5px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.clear-file-button {
  background-color: #e74c3c;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 3px;
  cursor: pointer;
}

.clear-file-button:hover {
  background-color: #c0392b;
}

.error-message {
  color: #e74c3c;
  margin-bottom: 20px;
  padding: 10px;
  background-color: #fdf2f2;
  border: 1px solid #f5c6cb;
  border-radius: 5px;
}

.form-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
}

.cancel-button,
.submit-button {
  padding: 12px 30px;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.cancel-button {
  background-color: #95a5a6;
  color: white;
}

.cancel-button:hover:not(:disabled) {
  background-color: #7f8c8d;
}

.cancel-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.submit-button {
  background-color: #27ae60;
  color: white;
}

.submit-button:hover:not(:disabled) {
  background-color: #219653;
}

.submit-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
</style>